from django.shortcuts import render,redirect
from django.http import HttpResponse
from .models import ArticlePost
from .forms import ArticlePostForm
from django.contrib.auth.models import User
from django import forms
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from django.db.models import Q
from django.contrib.auth.decorators import login_required

#不分页的list页面
# def article_list(request):
#     articles = ArticlePost.objects.all()
    
#     context = {'articles':articles }

#     return render(request,'article/list.html',context)

def article_list(request):
    articles = ArticlePost.objects.all()
    paginator = Paginator(articles, 5) #分页

    page = request.GET.get('page')
    try:
        current_num = int(request.GET.get('page',1))  #当你在url内输入的?page = 页码数  显示你输入的页面数目 默认为第2页
        articles = paginator.page(current_num)
    except EmptyPage:
        articles = paginator.page(4)  #当你输入的page是不存在的时候就会报错

    # if paginator.num_pages > 11:  # 如果分页的数目大于11
    #     if current_num - 5 < 1:  # 你输入的值
    #         pageRange = range(1,11)  # 按钮数
    #     elif current_num + 5 > paginator.num_pages:  # 按钮数加5大于分页数
    #         pageRange = range(current_num - 5, current_num + 1)  # 显示的按钮数

    #     else:
    #         pageRange = range(current_num - 5, current_num + 6)  # range求的是按钮数   如果你的按钮数小于分页数 那么就按照正常的分页数目来显示
    #  else:
    #      pageRange = paginator.page_range()  # 正常分配

    return render(request,'article/list.html',{'articles':articles})
    

def article_detail(request,id):
    article = ArticlePost.objects.get(id=id)

    context = {'article':article }

    return render(request,'article/detail.html',context)

@login_required
def article_create(request):
    if request.method == "POST":
        article_post_form = ArticlePostForm(request.POST,request.FILES) #添加了request.FILES
        if article_post_form.is_valid():
            new_article = article_post_form.save(commit=False)
            new_article.author = User.objects.get(id=request.user.id)#获取登录的用户id
            new_article.save()
            return redirect("article:article_list")
        else:
            return HttpResponse("表单内容有误，请重新填写。")
    else:
        article_post_form = ArticlePostForm()
        context = {'article_post_form':article_post_form }
        return render(request,'article/create.html',context)

@login_required
def article_delete(request,id):
    article = ArticlePost.objects.get(id=id)
    article.delete()
    return redirect("article:article_list")

@login_required
def article_update(request, id):
    article = ArticlePost.objects.get(id=id)
    if request.method == "POST":
        article_post_form = ArticlePostForm(data=request.POST)
        if article_post_form.is_valid():
            article.title = request.POST['title']
            article.body = request.POST['body']
            article.save()
            return redirect("article:article_detail",id=id)
        else:
            return HttpResponse("表单内容有误，请重新填写。")

    #如果是GET请求
    else:
        article_post_form = ArticlePostForm()
        context = { 'article':article,'article_post_form':article_post_form}
        return render(request,'article/update.html',context)


def article_search(request):
    keyword = request.GET.get('keyword')
    error_msg = ''

    if not keyword:
        error_msg = '请输入关键词'
        return render(request, 'article/errors.html', {'error_msg': error_msg})

    post_list = ArticlePost.objects.filter(Q(title__icontains=keyword)|Q(body__icontains=keyword))
    return render(request, 'article/results.html', {'error_msg': error_msg,'post_list': post_list})